package com.flame.workflow.re.service;

import java.util.ArrayList;
import java.util.List;

import com.flame.workflow.re.entity.FormComponent;
import com.flame.workflow.re.entity.Model;
import com.flame.workflow.re.entity.Node;
import com.flame.workflow.re.entity.Property;
import com.flame.workflow.re.mapper.PropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * flame_workflow_re_property 流程属性定义表
 *
 * @author 吴欣童
 * @since 2023-05-04 19:56:01
 */
@Service
public class PropertyService {

    @Autowired
    private PropertyMapper propertyMapper;

    public List<Property> getAllProperties(Model model, List<FormComponent> formItemList, List<Node> nodeList) {
        Long modelId = model.getModelId();
        Integer modelVersion = model.getModelVersion();

        List<Property> propertyList = new ArrayList<>();
        // 获取扩展设置属性信息
        propertyList.addAll(getObjectProperties(modelId, modelVersion, String.valueOf(modelId)));
        // 获得所有表单项属性信息
        for (FormComponent formItem : formItemList) {
            propertyList.addAll(getObjectProperties(modelId, modelVersion, formItem.getComponentCode()));
        }
        // 获得所有节点属性信息
        for (Node node : nodeList) {
            propertyList.addAll(getObjectProperties(modelId, modelVersion, String.valueOf(node.getNodeId())));
        }
        return propertyList;
    }

    public List<Property> getObjectProperties(Long modelId, Integer modelVersion, String objectId) {
        return propertyMapper.listByObjectId(modelId, modelVersion, objectId);
    }

    public void maintainPropertyList(Long modelId, Integer modelVersion, List<Property> propertyList) {
        propertyMapper.deleteByModelId(modelId, modelVersion);
        for (Property property : propertyList) {
            property.setModelVersion(modelVersion);
            propertyMapper.insert(property);
        }
    }
}
